package com.tech.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
	private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);

	public static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static String FIRST_TIME = "1970-01-01 08:00:01";

	public static Date strToDate(String str, String format) {
		Date date = null;
		try {
			DateFormat df = new SimpleDateFormat(format);
			date = df.parse(str);
		} catch (Exception e) {
			LOG.error("DateUtils strToDate :{}", e.toString());
		}
		return date;
	}

	public static String dateToString(Date date, String format) {
		String ret = "";
		try {
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			ret = formatter.format(date);
		} catch (Exception e) {
			LOG.error("DateUtils dateToString :{}", e.toString());
		}
		return ret;
	}

	public static Date formatDate(Date date, String formatStr) {
		Date ret = null;
		try {
			SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
			String dateStr = formatter.format(date);
			ret = formatter.parse(dateStr);
		} catch (Exception e) {
			LOG.error("DateUtils formatDate :{}", e.toString());
		}
		return ret;
	}

}
